package com.algorithm;

import java.util.Arrays;

/**
 * @Auther: zwg
 * @Date: 2019/1/18 14:37
 * @Description:
 */
public class AlgorithmTest2 {
    //当前索引下标
    private static int index = 0;
    //偏移量
    private static int ofset = 0;

    public static void main(String[] args) {
        int nums[] = {1,7,8,9,1, 1, 2,2,3,4,5,4,4,3,2,1};
        System.out.println("修改之前：");
        for (int num : nums) {
            System.out.print(num);
        }
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            for (int j = index; j < len; j++) {
                sort(nums);
            }
            ofset = ++index;
            if(index==len){
                break;
            }
        }
        int maxIndex = index;
        nums = Arrays.copyOf(nums,maxIndex);
        System.out.println();
        System.out.println("修改之后：");
        for (int num : nums) {
            System.out.print(num);
        }

    }

    //提供其他地方调用的方法
    public static int[] xxx(int[] nums){
        int len = nums.length;
        for (int i=0;i<len;i++) {
            if(ofset<len)
                sort(nums);
        }
        int maxIndex = index+1;
        return Arrays.copyOf(nums,maxIndex);
    }

    //排序算法
    public static void sort(int[] nums){
        if(nums.length==0)
            return;
        if(nums[index]<nums[ofset]){
            int y =  nums[index + 1];
            nums[index + 1] = nums[ofset];
            nums[ofset] = y;
            ofset++;
            if(nums[index]>nums[index+1]){
                int x = nums[index];
                nums[index]=nums[index+1];
                nums[index+1]=x;
            }
        }else{
            ofset++;
        }
    }
}
